home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 23 / Amiga Format AFCD23 (Feb 1998, Issue 107).iso / -seriously_amiga- / shareware / hardware / truesound / source / players / playmp3.c < prev   
C/C++ Source or Header  |  1997-12-01  |  4KB  |  191 lines

  1. /*
  2. Example mpeg layer3 player for truesound.device
  3. By Pontus Fuchs 1997
  4. */
  5.  
  6. #include <stdio.h>
  7.  
  8. #include <exec/exec.h>
  9. #include <dos/dos.h>
  10.  
  11. #include <proto/dos.h>
  12. #include <proto/exec.h>
  13.  
  14. #define BUFFSIZE 0x10000 /*Must be even*/
  15.  
  16. /*protos*/
  17. int startup(void);
  18. void closedown(void);
  19. int getargs(void);
  20. void mainloop(void);
  21. ULONG fillbuffer(struct IOStdReq *ioreq, char *buffer);
  22.  
  23. /*Globala variabler*/
  24. char *filename=0;
  25. struct RDArgs *args=0;
  26. struct MsgPort *msgport1;
  27. struct MsgPort *msgport2;
  28. struct IOStdReq *ioreq1;
  29. struct IOStdReq *ioreq2;
  30. int deviceopen=0;
  31. BPTR file=0;
  32.  
  33. char buffer1[BUFFSIZE];
  34. char buffer2[BUFFSIZE];
  35.  
  36. void main(void)
  37. {
  38.   if(startup())
  39.   {
  40.     printf("Example MPEG layer3-player for truesound.device\nCopyright © 1997 by Pontus Fuchs\n");
  41.     printf("Now playing: %s\n", filename);
  42.     mainloop();
  43.     printf("\n");
  44.   }
  45.   closedown();
  46. }
  47.  
  48. void mainloop(void)
  49. {
  50. ULONG sigrcvd;
  51. int done=1;
  52. ULONG buffsize1;
  53. ULONG buffsize2;
  54.  
  55.   buffsize1=fillbuffer(ioreq1, buffer1);
  56.   buffsize2=fillbuffer(ioreq2, buffer2);
  57.  
  58.   while(done)
  59.   {
  60.     sigrcvd=Wait(SIGBREAKF_CTRL_C|1<<msgport1->mp_SigBit|1<<msgport2->mp_SigBit);
  61.  
  62.     if(sigrcvd & SIGBREAKF_CTRL_C) /* ctrl_c? */
  63.       done=0;
  64.  
  65.     if(sigrcvd & (1<<msgport1->mp_SigBit))
  66.     {
  67.       WaitIO( (struct IORequest*) ioreq1);
  68.       buffsize1=fillbuffer(ioreq1, buffer1);
  69.       /*printf("Filled buffer1 with %d bytes\n", buffsize1);*/
  70.  
  71.     }
  72.  
  73.     if(sigrcvd & (1<<msgport2->mp_SigBit))
  74.     {
  75.       WaitIO( (struct IORequest*) ioreq2);
  76.       buffsize2=fillbuffer(ioreq2, buffer2);
  77.       /*printf("Filled buffer2 with %d bytes\n", buffsize2);*/
  78.     }
  79.  
  80.     if((buffsize1==0) & (buffsize2==0))  /*exit if both buffers are empty*/
  81.       done=0;
  82.  
  83.   }
  84.  
  85. /*Abort any active IO-requests*/
  86.   if(buffsize1)
  87.   {
  88.     AbortIO( (struct IORequest*) ioreq1);
  89.     WaitIO( (struct IORequest*) ioreq1);
  90.   }
  91.  
  92.   if(buffsize2)
  93.   {
  94.     AbortIO( (struct IORequest*) ioreq2);
  95.     WaitIO( (struct IORequest*) ioreq2);
  96.   }
  97.  }
  98.  
  99. ULONG fillbuffer(struct IOStdReq *ioreq, char *buffer)
  100. {
  101. ULONG readsize;
  102.  
  103.   readsize=Read(file, buffer, BUFFSIZE);
  104.   if(readsize==-1)
  105.   {
  106.     printf("Read error!\n");
  107.     return 0;
  108.   }
  109.  
  110.   if(readsize)
  111.   {
  112.     ioreq->io_Command=CMD_WRITE;
  113.     ioreq->io_Data=buffer;
  114.     ioreq->io_Length=readsize;
  115.     SendIO((struct IORequest*) ioreq);
  116.   }
  117.   
  118.   return readsize;
  119. }
  120.  
  121. int startup(void)
  122. {
  123.   if(!getargs())
  124.     return 0;
  125.  
  126.   if((msgport1=CreateMsgPort())==0)
  127.     return 0;
  128.   if((ioreq1=CreateIORequest(msgport1, sizeof(struct IOStdReq)))==0)
  129.     return 0;
  130.   if(OpenDevice("truesound.device", 0, (struct IORequest*) ioreq1, 0))
  131.     return 0;
  132.   deviceopen++;
  133.  
  134.   if((msgport2=CreateMsgPort())==0)
  135.     return 0;
  136.   if((ioreq2=CreateIORequest(msgport2, sizeof(struct IOStdReq)))==0)
  137.     return 0;
  138.   if(OpenDevice("truesound.device", 0, (struct IORequest*) ioreq2, 0))
  139.     return 0;
  140.   deviceopen++;
  141.  
  142.   if((file=Open(filename, MODE_OLDFILE))==0)
  143.   {
  144.     printf("Error: Could't open %s\n",filename);
  145.     return 0;
  146.   }
  147.  
  148.   return 1;
  149. }
  150.  
  151. void closedown(void)
  152. {
  153.   if(file)
  154.     Close(file);
  155.  
  156.   if(deviceopen) /*Check if IOreq1 was opened*/
  157.     {
  158.       CloseDevice((struct IORequest*) ioreq1);
  159.       deviceopen--;
  160.     }
  161.  
  162.   if(deviceopen) /*Check if IOreq2 was opened*/
  163.     CloseDevice((struct IORequest*) ioreq2);
  164.  
  165.   DeleteIORequest(ioreq1); /*passing NULL is safe*/
  166.   DeleteMsgPort(msgport1);
  167.   DeleteIORequest(ioreq2);
  168.   DeleteMsgPort(msgport2);
  169.  
  170.   FreeArgs(args);
  171. }
  172.  
  173. int getargs(void)
  174. {
  175. LONG array[2]={0,};
  176.  
  177.   if(args=ReadArgs("FILE/A",array,NULL))
  178.   {
  179.     if(array[0])
  180.       filename=(char*)array[0];
  181.   }
  182.  
  183.   if(filename==0)
  184.   {
  185.     printf("Play WHAT?\n");
  186.     return 0;
  187.   }
  188.   else
  189.     return 1;
  190. }
  191.